//
//  VRHomeSearchViewController.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/17.
//

import UIKit

class VRHomeSearchViewController: VRBaseViewController {

    private let cancelButton = c_make_ex(UIButton.self)
        .text("Cancel").color("#FFFC00").font(14.font_bold).end()
    private let searchTextField         = c_make_ex(UITextField.self)
        .font(16.font_medium).color(.white).corner(17).bgColor(.white.alpha(0.1)).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .font(16.font_semiBold).color(.white)
        .text("Everyone is searching").end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private var dataList:[VRTagItemModel] = {
       let arr = ["All","Male","Female","Voice"]
        var data = [VRTagItemModel]()
        for str in arr {
            let model = VRTagItemModel(title: str, isSel: false)
            data.append(model)
        }
        return data
    }()
    
    
    init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .fullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            cancelButton
            searchTextField
            titleLabel
            collectionView
        }
        
        cancelButton.trailing(15).width(50).top(.statusHeight + 12)
        searchTextField.height(34).leading(15)
        searchTextField.CenterY == cancelButton.CenterY
        searchTextField.Trailing == cancelButton.Leading - 20
        titleLabel.leading(15)
        titleLabel.Top == searchTextField.Bottom + 35
        collectionView.leading(0).trailing(0).height(100)
        collectionView.Top == titleLabel.Bottom + 20
        
        searchTextField.style { v in
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
            let leftImg = UIImageView(image: "HomeSearch_search".toImage)
            leftImg.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
            leftV.addSubview(leftImg)
            v.leftView = leftV
            v.leftViewMode = .always
        }
        let layout = UICollectionViewLeftAlignedLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        layout.sectionInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(VRTagCollectionItem.self, forCellWithReuseIdentifier: VRTagCollectionItem.description())
            v.showsVerticalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
        
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
    }

    @objc private func clickCancelButton(){
        self.dismiss(animated: true)
    }

}

extension VRHomeSearchViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: VRTagCollectionItem.description(), for: indexPath) as? VRTagCollectionItem else { return UICollectionViewCell() }
        item.model = dataList[indexPath.row]
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        var models = [VRTagItemModel]()
        dataList.forEach { model in
            var itemModel = model
            itemModel.isSel = false
            models.append(itemModel)
        }
        models[indexPath.row].isSel = true
        dataList = models
        collectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let model = dataList[indexPath.row]
//        var w = model.title.caculateWidth(CGSize(width: 85, height: 30), 14.font_medium!)
        var w = (model.title as NSString).size(withAttributes: [NSAttributedString.Key.font: 14.font_medium!]).width
        w = w + 20.0
        return CGSize(width: w, height: 30)
    }
}
